package zhou.contest14;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDao {
	BaseDao bd=new BaseDao();
	Connection con=null;
	PreparedStatement ps=null;
	ResultSet rs=null;
	
	//通过用户名查找数据库得用户信息
	public User getUserbyName(String name) throws SQLException{
		con=bd.getCon();
		String sql="select *from user where username=?";
		ps=con.prepareStatement(sql);
		ps.setString(1, name);
		rs=ps.executeQuery();
		String username=null;
		String pwd=null;
		while(rs.next()){
			username=rs.getString("username");
			pwd=rs.getString("pwd");
		}
		User user=new User(username,pwd);
		return user;
	}
	
	//获取用户阅读文章后的信息
	public boolean read(String username,String evaluate,String message) throws SQLException{
		boolean result=false;
		con=bd.getCon();
		String sql1="insert into read (username) values('"+username+"')";
		ps=con.prepareStatement(sql1);
		if(ps.executeUpdate()>0){
			String sql2="insert into read (evaluate,message) values('"+evaluate+"','"+message+"') where read.username='"+username+"'";
			ps=con.prepareStatement(sql2);
			if(ps.executeUpdate()>0){
				result=true;				
			}
			else{
				System.out.println("评价或留言不成功");
				return result;
			}
		}
		else{
			System.out.println("用户名添加不成功");
			return result;
		}
		
		closeData();
		return result;
	}
	
	
	//关闭数据库
		public void closeData() throws SQLException{
			if(rs!=null){
				rs.close();
			}
			if(ps!=null){
				ps.close();
			}
			if(con!=null){
				con.close();
			}
		}
}
